package cn.kgc.springboot.exception;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2022/8/10
 */
@RestControllerAdvice
public class GlobalExceptionHandler  {


    @ExceptionHandler(UnknownAccountException.class)
    public Map  unKnowAccountExceptionHandler (UnknownAccountException e){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", "用户名错误");
        return  map;
    }


    @ExceptionHandler(IncorrectCredentialsException.class)
    public Map  incorrectCredentialsExceptionHandler (IncorrectCredentialsException e){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", "密码错误");
        return  map;
    }

    @ExceptionHandler(AuthorizationException.class)
    public Map  AuthorizationExceptionHandler (AuthorizationException e){
        e.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 403);
        map.put("msg", "没有权限请联系管理员!!");
        return  map;
    }

}
